<?php
/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 2017/8/1
 * Time: 上午11:14
 */

namespace app\controllers;


use app\librarys\BaseController;
use app\models\Ad;
use app\models\File;
use app\models\Merchant;
use yii\data\Pagination;

class AdController extends BaseController
{
    // region 广告行业类型

    public function actionAdIndustryTypeList()
    {
        $count = Ad::getAdIndustryTypeCount();
        $pagination = new Pagination(['totalCount' => $count]);
        $items = Ad::getAdIndustryTypeList($pagination->page, $pagination->pageSize);
        return $this->render('ad-industry-type-list',
            [
                'items' => $items,
                'pagination' => $pagination
            ]
        );
    }

    public function actionAdIndustryTypeDetailPartial()
    {
        $id = \Yii::$app->request->post('id');
        $item = null;
        if ($id != '0') {
            $item = Ad::getAdIndustryTypeById($id);
        }
        return $this->renderPartial('ad-industry-type-detail-partial',
            ['id' => $id, 'item' => $item]);
    }

    public function actionAdIndustryTypeSaveHandler()
    {
        $id = \Yii::$app->request->post('id');
        $name = \Yii::$app->request->post('name');
        $isSpecial = \Yii::$app->request->post('is_special');

        Ad::saveAdIndustryType($id, $name, $isSpecial);

        return $this->renderJson();
    }

    public function actionAdIndustryTypeDeleteHandler()
    {
        $id = \Yii::$app->request->post('id');

        Ad::deleteAdIndustryTypeById($id);

        return $this->renderJson();
    }
    // endregion

    // region 广告管理
    public function actionAdList()
    {
        $count = Ad::getAdCount();
        $pagination = new Pagination(['totalCount' => $count]);
        $items = Ad::getAdList($pagination->page, $pagination->pageSize);

        return $this->render('ad-list',
            [
                'items' => $items,
                'pagination' => $pagination
            ]
        );
    }

    public function actionAdDetailPartial()
    {
        $industryItems = Ad::getAdIndustryTypeAllList();

        $id = \Yii::$app->request->post('id');
        $item = null;
        if ($id != '0') {
            $item = Ad::getAdById($id);
            $item['show_image'] = Ad::getAdShowImage($id);
            $item['detail_images'] = Ad::getAdDetailImages($id);
        }

        return $this->renderPartial('ad-detail-partial', [
            'industryItems' => $industryItems,
            'id' => $id,
            'item' => $item
        ]);
    }

    public function actionAdSaveHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('hiddenId');
        $merchant_id = Merchant::getCurrentMerchant()['id'];
        $industry_type_id = $request->post('industryType');
        $title = $request->post('inputTitle', '');
        $content = $request->post('inputContent', '');
        $status = '0';

        $detail_images_ids = $request->post('detail_images_ids', []);
        if (!empty($_FILES['previewMany'])) {
            $imgCoordinates = ($request->post('imgCoordinate'));
            $imgCoordinatesData = [];
            foreach ($imgCoordinates as $imgCoordinate) {
                $all = json_decode($imgCoordinate);
                $imgCoordinatesData[] = [
                    'x' => $all->x,
                    'y' => $all->y,
                    'w' => $all->w,
                    'h' => $all->h,
                    'size_w' => $all->size_w,
                    'size_h' => $all->size_h,
                ];
            }
            for ($i = 0; $i < count($_FILES['previewMany']['name']); $i++) {
                $data = [
                    'name' => $name = $_FILES['previewMany']['name'][$i],
                    'type' => $type = $_FILES['previewMany']['type'][$i],
                    'tmp_name' => $tmp_name = $_FILES['previewMany']['tmp_name'][$i],
                    'error' => $error = $_FILES['previewMany']['error'][$i],
                    'size' => $size = $_FILES['previewMany']['size'][$i],

                ];
                $imgCoordinate = $imgCoordinatesData[$i];
                $image_id = File::CutUploadImage($data, $imgCoordinate['x'], $imgCoordinate['y'], $imgCoordinate['w'], $imgCoordinate['h'], $imgCoordinate['size_w'], $imgCoordinate['size_h']);
                if (!$image_id) {
                    return $this->renderJson('', 1, '图片上传错误');
                }
                array_push($detail_images_ids, $image_id);
            }
        }

        $show_image_id = $request->post('show_image_id');
        if (!empty($_FILES['uploadImg']['name'])) {
            if ($_FILES['uploadImg']['error'] == 0) {
                $imgCord = json_decode($request->post('imgCord'));
                if ($imgCord) {
                    $show_image_id = File::CutUploadImage($_FILES['uploadImg'], $imgCord->x, $imgCord->y, $imgCord->w, $imgCord->h, $imgCord->size_w, $imgCord->size_h);
                }
                if (!$show_image_id) {
                    return $this->renderJson('', 1, '图片上传错误');
                }
            }
        }


        $result = Ad::saveAd($id, $merchant_id, $industry_type_id, $title, $content, $status, $detail_images_ids, $show_image_id);

        return $this->renderJson(null, $result ? 0 : 1, $result ? 'success' : 'fail');
    }

    public function actionAdDeleteHandler()
    {
        $id = \Yii::$app->request->post('id');

        Ad::deleteAdById($id);

        return $this->renderJson();
    }
    // endregion

    // region 广告举报
    public function actionAdAccuseList()
    {
        $count = Ad::getAdAccuseCount();
        $pagination = new Pagination(['totalCount' => $count]);
        $items = Ad::getAdAccuseList($pagination->page, $pagination->pageSize);
        return $this->render('ad-accuse-list',
            [
                'items' => $items,
                'pagination' => $pagination
            ]
        );
    }
    // endregion

    // region 广告审核
    public function actionAdAuthList()
    {
        $count = Ad::getAdAuthCount();
        $pagination = new Pagination(['totalCount' => $count]);
        $items = Ad::getAdAuthList($pagination->page, $pagination->pageSize);

        return $this->render('ad-auth-list',
            [
                'items' => $items,
                'pagination' => $pagination
            ]
        );
    }

    public function actionAdAuthDetailPartial()
    {
        $id = \Yii::$app->request->post('id');
        $item = null;
        if ($id != '0') {
            $item = Ad::getAdById($id);
            $item['show_image'] = Ad::getAdShowImage($id);
            $item['detail_images'] = Ad::getAdDetailImages($id);
            $item['special']=Ad::getAdSpecialInfo($id);
        }

        return $this->renderPartial('ad-auth-detail-partial', [
            'id' => $id,
            'item' => $item
        ]);
    }

    public function actionAdAuthSuccessHandler()
    {
        $adId = \Yii::$app->request->post('ad_id');
        $authRemark = \Yii::$app->request->post('auth_remark');
        Ad::authAd($adId, true, $authRemark);
        return $this->renderJson();
    }

    public function actionAdAuthFailedHandler()
    {
        $adId = \Yii::$app->request->post('ad_id');
        $authRemark = \Yii::$app->request->post('auth_remark');
        Ad::authAd($adId, false, $authRemark);
        return $this->renderJson();
    }

    // endregion
}